#include "colorpicker.h"

ColorPicker::ColorPicker(QWidget *parent)
	: QWidget(parent)
{
	this->setMaximumSize(300, 300);
	this->setMinimumSize(300, 120);
	this->setWindowTitle("Color Picker");

	QGridLayout* layout = new QGridLayout();

	current = QColor(128, 128, 128);

	red = new QSlider(Qt::Horizontal, this);
	green = new QSlider(Qt::Horizontal, this);
	blue = new QSlider(Qt::Horizontal, this);
	
	//set sliders
	green->setMinimum(0);
	green->setMaximum(255);
	green->setSingleStep(1);
	green->setValue(current.green());

	blue->setMinimum(0);
	blue->setMaximum(255);
	blue->setSingleStep(1);
	blue->setValue(current.blue());

	red->setMinimum(0);
	red->setMaximum(255);
	red->setSingleStep(1);
	red->setValue(current.red());

	pixmap = new QPixmap(80,80);
	pixmap->fill( current );
	display = new QLabel(this);
	display->setPixmap(*pixmap);

	choose = new QPushButton("Done", this);
	aggregate = new QPushButton("Aggregate", this);
	undo = new QPushButton("Cancel", this);

	layout->addWidget(red, 0, 0, 1, 1);
	layout->addWidget(green, 1, 0, 1, 1);
	layout->addWidget(blue, 2, 0, 1, 1);
	layout->addWidget(display, 0, 1, 2, 1);
	layout->addWidget(choose, 2, 1, 1, 1);
	layout->addWidget(aggregate, 3, 1, 1, 1);
	layout->addWidget(undo, 3, 0, 1, 1);

	this->setLayout(layout);

	connect( red, SIGNAL(valueChanged(int)), this, SLOT(updateColor()) ); 
	connect( green, SIGNAL(valueChanged(int)), this, SLOT(updateColor()) );
	connect( blue, SIGNAL(valueChanged(int)), this, SLOT(updateColor()) );

	connect( choose, SIGNAL(clicked()), this, SLOT(finalize()) );
	connect( aggregate, SIGNAL(clicked()), this, SLOT(merge()) );
	connect( undo, SIGNAL(clicked()), this, SLOT(cancel()) );

}

ColorPicker::~ColorPicker()
{

}

void ColorPicker::updateColor() {

	//store color
	current = QColor( red->value(), green->value(), blue->value() );
	//fill pixmap with current color
	delete pixmap;
	pixmap = new QPixmap(80, 80);
	pixmap->fill(current);
	//update label
	display->setPixmap(*pixmap);
	display->update();
}


void ColorPicker::finalize() {

	//emit two signals, one for the color and one because the job is done, then 
	//hide the window
	emit sendColor(current);

	this->hide();
}

void ColorPicker::merge() {
	emit mergeCluster();
	this->hide();
}

void ColorPicker::cancel() {
	emit removeCluster();
	this->hide();
}